package com.dinadi.yidi.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.dinadi.yidi.R;
import com.dinadi.yidi.bean.ProductSubjectListBean;
import com.dinadi.yidi.ui.activity.ProductDetailsActivity;
import com.dinadi.yidi.utils.ImageUtils;

import java.util.List;

/**
 * author: JiaYu.He
 * Date: 2018/5/18
 * Time: 11:31
 */

public class ItemDesignerHomePageAdapter2 extends BaseAdapter {
    private List<ProductSubjectListBean>list;
    private Context mContext;

    public ItemDesignerHomePageAdapter2(Context context, List<ProductSubjectListBean> list) {
        this.list = list;
        this.mContext = context;
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.adapter_creative_product, null);
            viewHolder = new ViewHolder();
            viewHolder.img_product = (ImageView) convertView.findViewById(R.id.img_product);
            viewHolder.nick_name = (TextView) convertView.findViewById(R.id.nick_name);
            viewHolder.nick_name_price = (TextView) convertView.findViewById(R.id.nick_name_price);
            viewHolder.original_price = (TextView) convertView.findViewById(R.id.original_price);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        String imgUrl = list.get(position).getCoverImg();
        ImageUtils.loadImageView(mContext, imgUrl, viewHolder.img_product);
        viewHolder.nick_name.setText(list.get(position).getTitle());
        String price = list.get(position).getPrice();
        String originalPrice = list.get(position).getOriginalPrice();
        viewHolder.nick_name_price.setText("￥" + price);
        if (!price.equals(originalPrice)) {
            viewHolder.original_price.setText("￥" + originalPrice);
            viewHolder.original_price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); //中划线
        } else {
            viewHolder.original_price.setText("");
        }
        viewHolder.img_product.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, ProductDetailsActivity.class);
                intent.putExtra("productId", list.get(position).getProductId());
                mContext.startActivity(intent);
            }
        });
        return convertView;
    }


    class ViewHolder {

        ImageView img_product;
        TextView nick_name, nick_name_price, original_price;

    }
}
